package cn.whao.guli.stock.mapper;

import cn.whao.guli.stock.entity.StockEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;

/**
 * ClassName: StockMapper
 * Package: cn.whao.guli.stock.mapper
 * Description:
 *
 * @Author 王豪
 * @Create 2024/11/26 15:47
 * @Version 1.0
 */
public interface StockMapper extends JpaRepository<StockEntity, Integer> {

    //Spring Data JPA会根据我们所定义的⽅法名，⾃动⽣成对应的sql语句(方法结构需要满足要求)
    //文档地址: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/
    //@Query：自定义sql实现查询，@Modifying：指定sql为更新,不添加此注解表示查询
    @Transactional //更新需要添加事务注解
    @Modifying  // 表示本次操作是更新
    @Query("update StockEntity s set s.count = s.count - ?2 where s.productId = ?1")
    int updateCountByProductId(String productId, Integer count);
}
